package com.dhwl.hsh.merchant.ui.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.dhwl.hsh.lib.bean.resp.OrderListResponse;
import com.dhwl.hsh.lib.core.HshException;
import com.dhwl.hsh.merchant.R;
import com.dhwl.hsh.merchant.common.Constant;
import com.dhwl.hsh.merchant.presenter.OrderPresenter;
import com.dhwl.hsh.merchant.presenter.iview.IGetOrdersView;
import com.dhwl.hsh.merchant.ui.adapter.OrderListAdapter;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;


/**
 * 订单列表
 *
 * @author Derek.Kidd
 * @time 2016/4/22 16:09
 */
public class OrderListFragment extends BaseFragment implements IGetOrdersView {

    @Bind(R.id.pull_refresh_list)
    PullToRefreshListView pullToRefreshListView;

    @Bind(R.id.layout_empty)
    LinearLayout emptyLayout;
    @Bind(R.id.layout_net_error)
    LinearLayout netErrorLayout;

    @Bind(R.id.btn_reload)
    Button btnReload;

    OrderListAdapter orderListAdapter;

    List<OrderListResponse> orderListResps;

    OrderPresenter orderPresenter;

    int orderStatus;

    int pageNo = 1;


    public static OrderListFragment newInstance(int position) {
        OrderListFragment orderListFragment = new OrderListFragment();
        Bundle bundle = new Bundle();
        switch (position) {
            //待配送
            case 0:
                bundle.putInt(Constant.Order.VALUE_ORDER_STATUS, Constant.Order.VALUE_ORDER_STATUS_1);
                break;
            //待确认
            case 1:
                bundle.putInt(Constant.Order.VALUE_ORDER_STATUS, Constant.Order.VALUE_ORDER_STATUS_2);
                break;
            //待评论
            case 2:
                bundle.putInt(Constant.Order.VALUE_ORDER_STATUS, Constant.Order.VALUE_ORDER_STATUS_3);
                break;
            //全部
            case 3:
                bundle.putInt(Constant.Order.VALUE_ORDER_STATUS, Constant.Order.VALUE_ORDER_STATUS_0);
                break;
        }
        orderListFragment.setArguments(bundle);
        return orderListFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        orderStatus = getArguments().getInt(Constant.Order.VALUE_ORDER_STATUS);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_order_list;
    }

    @Override
    protected void initViews(View self, Bundle savedInstanceState) {

    }

    @Override
    protected void initData() {
        orderPresenter = new OrderPresenter();
        orderPresenter.attachView(this);
        orderPresenter.getOrders(pageNo, orderStatus);

        orderListResps = new ArrayList<>();

        orderListAdapter = new OrderListAdapter(getActivity(), orderListResps, orderPresenter);
        pullToRefreshListView.setAdapter(orderListAdapter);

        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                //下拉刷新
                pageNo = 1;
                orderListAdapter.clear();
                orderPresenter.getOrders(pageNo, orderStatus);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                //加载更多
                pageNo++;
                orderPresenter.getOrders(pageNo, orderStatus);
            }
        });
    }

    /**
     * 显示空页面
     */
    public void showEmptyPage() {
        netErrorLayout.setVisibility(View.GONE);
        pullToRefreshListView.setVisibility(View.GONE);
        emptyLayout.setVisibility(View.VISIBLE);
    }

    /**
     * 显示无网页面
     */
    public void showNetErrorPage() {
        emptyLayout.setVisibility(View.GONE);
        pullToRefreshListView.setVisibility(View.GONE);
        netErrorLayout.setVisibility(View.VISIBLE);
    }

    /**
     * 显示内容数据页面
     */
    public void showContentPage() {
        emptyLayout.setVisibility(View.GONE);
        netErrorLayout.setVisibility(View.GONE);
        pullToRefreshListView.setVisibility(View.VISIBLE);
    }


    @OnClick(R.id.btn_reload)
    public void onReloadClick(View view) {
        pageNo = 1;
        orderListAdapter.clear();
        orderPresenter.getOrders(pageNo, orderStatus);
    }

    @Override
    public void onGetOrdersFailure(HshException e) {

    }

    @Override
    public void onGetOrdersSuccess(List<OrderListResponse> orderListResponses) {
        pullToRefreshListView.onRefreshComplete();
        orderListAdapter.addAll(orderListResponses);
    }

    @Override
    public void onCancelOrderFalure(HshException e) {

    }

    @Override
    public void onCancelOrderSuc() {

    }
}
